<?php	
    include_once(dirname(__FILE__). "/../../../managers/class.User_Manager.php");
    include_once(dirname(__FILE__). "/../../../managers/class.Order_Manager.php");
    include_once(dirname(__FILE__). "/../../../managers/class.Parameters_Manager.php");
    include_once(dirname(__FILE__). "/../../../treatments/class.Log.php");

    class OrderAdministration_Presenter{

        private $user_manager;
        private $order_manager;
        private $param_manager;
        
        private $order_administrators;

        public function __construct($userId){
            //--initialisation des attibuts
            $this->user_manager = new User_Manager($userId, "../../");
            $this->order_manager = new Order_Manager($userId, "../../");
            $this->param_manager = new Parameters_Manager();
            
            $this->order_administrators = array(array());

            Log::ajoutLignePage($userId, "Administration: Orders", "../../");
        }
        
        public function getUserPictureDirectory(){
            return $this->param_manager->getUserPictureDirectory();
        }
        
        public function getOrderPictureDirectory(){
            return $this->param_manager->getOrderPicturesDirectory();
        }
        
        public function getDirTemp(){
               return $this->param_manager->getTempDirectory();
        }
        
        public function getAllAuthorizedOrder($user_id){
           $all_orders = $this->getAllOrder();
           $all_orders[] = new Ordre(0, "", "default_order.png");
           
           $current_user = $this->user_manager->getUserById($user_id);
           
           //--Si l'utilisateur est administrateur global, alors il peut voir tous les ordres
           if($current_user->getAdmin() == 1){
               return $all_orders;
           }
           //-Sinon on recherche tous les ordres pour lesquels il estadministrateur
           else{
               $retour = array();
               //-Pour chaque ordre
               foreach ($all_orders as $order) {
                   //--Pour chaque admin de chaque ordre
                   foreach($this->getAdminToOrder($order->getId()) as $admin){
                       if($admin != "badLog"){
                            if($admin->getId() == $current_user->getId()){
                                $retour[] = $order;
                            }
                       }
                   }
               }
               return $retour;
           }
        }
        
        private function getAllOrder(){
            $orders = $this->order_manager->getAllOrders();
            //--Pour chaque ordre
            foreach($orders as $order){
                //--Pour chaque id administrateurs
                foreach($order->getIdAdministrateurs() as $adminid){
                    $this->order_administrators[$order->getId()][] = $this->user_manager->getUserById($adminid);
                }
            }
            return $orders;
        }
        
        public function getAdminToOrder($orderId){
            return $this->order_administrators[$orderId];
        }
        
        public function getAllUsers(){
            return $this->user_manager->getAllUser(true);
        }
        
        public function upgradeAdministrator($userId, $orderId){
            return $this->order_manager->setAdminToOrder($userId, $orderId);
        }
        
        public function downgradeAdministrator($userId, $orderId){
            return $this->order_manager->unSetAdminToOrder($userId, $orderId);
        }
        
        public function getIfUserIsOrderAdmin($user_id){
                $res = $this->user_manager->getAllOrderAdmin();
                $est_admin = false;
                foreach ($res as $r) {
                    if($r['id_user'] == $user_id){
                        $est_admin = true;
                    }
                }
                return $est_admin;
        }
        
        public function addOrder($name, $picture){
            
            if($picture != "default_order.png"){
                //--R�cup�re l'extension
                $spliter = explode('.', $picture);
                $extension = $spliter[count($spliter)-1];
                $picture_name = $name."_order.".$extension;

                //--Copie de la photo dans le r�pertoire
                $rep_order_picture = "../../".$this->param_manager->getOrderPicturesDirectory();
                $rep_temporaire = "../../".$this->getDirTemp();

                //@unlink($rep_order_picture."/".$current_order->getLienImage());
                $photo_ok = false;
                if(@copy($rep_temporaire.$picture, $rep_order_picture."/".$picture_name)){
                    $photo_ok = true;
                }
            }
            else {
                $picture_name = $picture;
            }
            return $this->order_manager->addOrder($name, $picture_name);
        }
        
        public function updateOrder($id, $name, $picture){
            
            $current_order = $this->order_manager->getOrderById($id);
            
            //--Update del'image
            if($picture != $current_order->getLienImage()){
                
                //--R�cup�re l'extension
                $spliter = explode('.', $picture);
                $extension = $spliter[count($spliter)-1];
                $picture_name = $name."_order.".$extension;

                //--Copie de la photo dans le r�pertoire
                $rep_order_picture = "../../".$this->param_manager->getOrderPicturesDirectory();
                $rep_temporaire = "../../".$this->getDirTemp();

                if($current_order->getLienImage() != "default_order.png"){
                    @unlink($rep_order_picture."/".$current_order->getLienImage());
                }
                $photo_ok = false;
                if(@copy($rep_temporaire.$picture, $rep_order_picture."/".$picture_name)){
                    $photo_ok = true;
                }
            }
            else {
                $picture_name = $picture;
            }
            return $this->order_manager->updateOrder($id, $name, $picture_name);
        }
        
        public function orderIsDeletable($order_id){
            return $this->order_manager->orderIsDeletable($order_id);
        }
        
        public function delOrder($order_id){
            
            $current_order = $this->order_manager->getOrderById($order_id);
            $rep_order_picture = "../../".$this->param_manager->getOrderPicturesDirectory();
            
            if($current_order->getLienImage() != "default_order.png"){
                @unlink($rep_order_picture."/".$current_order->getLienImage());
            }
            
            return $this->order_manager->deleteOrder($order_id);
        }
    }